/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import { expect } from '@jest/globals';
import * as fse from 'fs-extra';
import * as path from 'path';

import { ProjectBuildProfile } from '../../../../src/options/build/project-build-profile.js';
import { SignUtil } from '../../../../src/tasks/sign/sign-util.js';
import MaterialBuildOpt = ProjectBuildProfile.MaterialBuildOpt;

test('test getDefaultSign', () => {
  const material = SignUtil.getDefaultSign().material;

  expect(material.keyPassword).toEqual('DebugKey');
  expect(material.storePassword).toEqual('DebugKey');
  expect(material.keyAlias).toEqual('debugKey');

  const storeFile = material.storeFile;
  expect(fse.existsSync(storeFile)).toBe(true);
});

test('test isUseDefaultSign', () => {
  const emptyMaterial: MaterialBuildOpt = {
    storeFile: '',
    keyPassword: '',
    storePassword: '',
    signAlg: '',
    keyAlias: '',
    profile: '',
    certpath: ''
  };
  expect(SignUtil.isUseDefaultShellSign(emptyMaterial)).toBe(false);

  const userDir = process.env.HOME || process.env.USERPROFILE;
  const myDefaultMaterial: MaterialBuildOpt = {
    storeFile: path.resolve(userDir!, '.ohos', 'config', 'debug.p12'),
    keyPassword: 'DebugKey',
    storePassword: 'DebugKey',
    signAlg: 'anythings',
    keyAlias: 'debugKey',
    profile: 'anythings',
    certpath: 'anythings'
  };
  expect(SignUtil.isUseDefaultShellSign(myDefaultMaterial)).toBe(true);
});
